/**
 * 
 */
package controller;

import java.util.List;
import java.util.Map;

import javax.mail.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;

import exception.ApplicationException;

import model.Administrator;
import model.Door;
import model.Log;
import model.Message;
import service.SecurityQueryHelper;
import service.SecurityService;
import service.SecurityServiceImpl;
import utils.Page;

/**
 * 
 * @author yalin
 * 
 */
public class SecurityAction implements SessionAware {
	private Map<String, Object> session;

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	private Door door;
	private Administrator admin;
	private List<Door> doorList;
	private Message msg;
	private Log log;
	private SecurityQueryHelper helper = null;
	private Page page = null;

	public Door getDoor() {
		return door;
	}

	public void setDoor(Door door) {
		this.door = door;
	}

	public Administrator getAdmin() {
		return admin;
	}

	public void setAdmin(Administrator admin) {
		this.admin = admin;
	}

	public List<Door> getDoorList() {
		return doorList;
	}

	public void setDoorList(List<Door> doorList) {
		this.doorList = doorList;

	}

	public Message getMsg() {
		return msg;
	}

	public void setMsg(Message msg) {
		this.msg = msg;
	}

	public Log getLog() {
		return log;
	}

	public void setLog(Log log) {
		this.log = log;
	}

	public SecurityQueryHelper getHelper() {
		return helper;
	}

	public void setHelper(SecurityQueryHelper helper) {
		this.helper = helper;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String home() {
		return "homePage";
	}

	public String input() {

		return "inputPage";
	}

	public String loadAllLog() {

		if (page == null) {
			page = new Page();

		}
		SecurityService sas = new SecurityServiceImpl();
		doorList = sas.loadAllDoor();
		if (helper != null) {
			helper.setType("log");
		} else {
			helper = new SecurityQueryHelper();
			helper.setType("log");
		}
		page = sas.loadPagedLogs(page, helper);

		return "loadPagedLogPage";
	}

	public String forUpdateDoor() {
		SecurityService sas = new SecurityServiceImpl();
		door = sas.getDoorByID(door.getDoor_ID());

		return "updatePage";
	}

	public String update() {
		SecurityService sas = new SecurityServiceImpl();
		admin = (Administrator) session.get("loginedAdmin");
		try {
			sas.updateDoor(door, admin);
		} catch (ApplicationException e) {
			e.printStackTrace();
		}
		System.out.println("update successful!");
		return "success";
	}

	public String updateAll() {
		SecurityService sas = new SecurityServiceImpl();
		try {
			sas.updateAll();
		} catch (ApplicationException e) {
			e.printStackTrace();
		}
		System.out.println("updateall successful!");
		return "success";
	}

	public String loadErrorReport() {
		if (page == null) {
			page = new Page();

		}
		SecurityService sas = new SecurityServiceImpl();

		if (helper == null) {
			helper = new SecurityQueryHelper();
		}
		helper.setType("message");
		helper.setMsg_type("Error");
		page = sas.loadPagedMsgs(page, helper);
		return "loadPagedErrorPage";
	}

	public String loadFireReport() {
		if (page == null) {
			page = new Page();

		}
		SecurityService sas = new SecurityServiceImpl();

		if (helper == null) {
			helper = new SecurityQueryHelper();
		}
		helper.setType("message");
		helper.setMsg_type("Fire");
		page = sas.loadPagedMsgs(page, helper);
		return "loadPagedFirePage";
	}

	public String readMsg() {
		admin = (Administrator) session.get("loginedAdmin");
		SecurityService sas = new SecurityServiceImpl();
		msg=sas.getMsgByID(msg.getMsg_ID());
		if(StringUtils.isEmpty(msg.getReceiver_ID())){
			msg.setReceiver_ID(admin.getAdmin_ID());
			sas.updateMsg(msg);
		}
		if(msg.getMsg_Type().equals("Fire")){
			return "firedetailPage";
		}
		if(msg.getMsg_Type().equals("Error")){
			return "errordetailPage";
		}
		return null;
		
	}

}
